package com.example.my_objectives;

import java.util.List;

import com.example.my_objectives.controller.NewObjectiveController;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TableRow;
import android.widget.Toast;


public class NewObjectiveActivity extends Activity {

	private NewObjectiveController _controller;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_objective);

		_controller = new NewObjectiveController(this);

		initializeView();

	}

	private void initializeView() {
		//***** Load the units spinner  *****
		Spinner spinnerUnits = (Spinner) findViewById(R.id.spNewObjectiveUnits);
		List <SpinnerObject> listUnits = _controller.getAllUnits();
		// Create an ArrayAdapter
		ArrayAdapter<SpinnerObject> adapterUnits = new ArrayAdapter<SpinnerObject>(this,
			    android.R.layout.simple_spinner_item, listUnits);
		// Specify the layout to use when the list of choices appears
		adapterUnits.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerUnits.setAdapter(adapterUnits);

		//***** Load the recurrence spinner  *****
		Spinner spinnerRecurrence = (Spinner) findViewById(R.id.spNewObjectiveRecurrence);
		List <SpinnerObject> listRecurrences = _controller.getAllRecurrences();
		// Create an ArrayAdapter using the string array and a default spinner layout
		//ArrayAdapter<CharSequence> adapterRecurrence = ArrayAdapter.createFromResource(this,
		//        R.array.planets_array, android.R.layout.simple_spinner_item);
		ArrayAdapter<SpinnerObject> adapterRecurrence = new ArrayAdapter<SpinnerObject>(this,
			    android.R.layout.simple_spinner_item, listRecurrences);
		// Specify the layout to use when the list of choices appears
		adapterRecurrence.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerRecurrence.setAdapter(adapterRecurrence);
	}

	public void swNewObjectiveNotifyMe_onClick(View view) {
		Switch switchNotifyMe = (Switch) findViewById(R.id.swNewObjectiveNotifyMe);

		TableRow tr7 = (TableRow) findViewById(R.id.tableRow7);
		TableRow tr8 = (TableRow) findViewById(R.id.tableRow8);

		if (switchNotifyMe.isChecked()) {
			tr7.setVisibility(TableRow.VISIBLE);
			tr8.setVisibility(TableRow.VISIBLE);
		}//if
		else {
			tr7.setVisibility(TableRow.GONE);
			tr8.setVisibility(TableRow.GONE);
		}//if
	}

	public void btnSave_onClick(View view) {
		//TODO: Save the new objective to the db
		Toast.makeText(NewObjectiveActivity.this, "Save the new objective to the db",
				Toast.LENGTH_SHORT).show();
	}


}
